package com.own.demo.长沙空中路网格子.v2;

import com.own.demo.geogrid.BoundingBoxUtil;
import com.own.demo.geogrid.UtilsGeoGrid;
import com.own.demo.utils.DateUtils;
import com.own.demo.utils.FileToStringUtils;
import com.own.demo.utils.geo.PoiTypeEnums;
import com.own.demo.vo.Bounds;
import com.own.demo.长沙空中路网格子.BuildingSelect;
import com.own.demo.长沙空中路网格子.VMainParam;
import org.locationtech.jts.geom.Geometry;
import org.opengis.geometry.BoundingBox;

import java.io.File;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class MainBuildV2 {
    //0.0007 70米左右 //0.00112  //0.00210 // 0.00310
    static double sideLen =0.06;

    public static ExecutorService executorService =newFixedThreadPool(10000);

    public static void main(String[] args) throws ParseException {
        Date sTime=new Date();
        System.out.println(DateUtils.parseDateToStr(new Date())+"-----全局解析数据完成："+(DateUtils.formatTime(sTime,new Date())));
        VMainParam vMainParam= vMainParam();
        System.out.println(DateUtils.parseDateToStr(new Date())+"-----公共参数完成："+(DateUtils.formatTime(sTime,new Date())));
        sTime=new Date();

        DrawEnter.goGeoJSONV2Local(vMainParam);
        System.out.println(DateUtils.parseDateToStr(new Date())+"-----路网数据："+(DateUtils.formatTime(sTime,new Date())));
        System.exit(-1);
    }

    public static  VMainParam vMainParam(){
        System.out.println("开始执行");
        File geoZJ = new File("E:\\ownCode\\emg-geojson - 副本\\src\\test\\java\\com\\own\\demo\\长沙空中路网格子\\device.txt");
        String geoData = FileToStringUtils.txt2String(geoZJ).split("\\|")[0];
        String[] adasStr = geoData.split("\\,");
        int row = adasStr.length;
        System.out.println("行政区域围栏点数量：" + row);
        BuildingSelect.init("E:\\ownCode\\emg-geojson - 副本\\src\\test\\java\\com\\own\\demo\\长沙空中路网格子\\building-40-up.json");

        List<Double> lonList = new ArrayList(100000);
        List<Double> latList = new ArrayList(100000);

        for (int i = 0; i < row; i++) {
            String[] points = adasStr[i].split(" ");
            int pointLength = points.length;
            if (pointLength > 2) {
                System.out.println("错误采集点");
                continue;
            }

            lonList.add(Double.valueOf(points[0]));
            latList.add(Double.valueOf(points[1]));
        }



        System.out.println("lon长度" + lonList.size());
        System.out.println("lat长度" + latList.size());

        double minLon = Collections.min(lonList);
        double maxLon = Collections.max(lonList);
        double minLat = Collections.min(latList);
        double maxLat = Collections.max(latList);
        Bounds bounds = new Bounds(minLon, maxLon, minLat, maxLat);
        try {
            BoundingBox boundingBox = BoundingBoxUtil.getBoundingBox(minLon, maxLon, minLat, maxLat);
            Geometry geometry = PoiTypeEnums.geometry(geoData, PoiTypeEnums.POLYGON.getCode());

            List<Geometry> geometries = new UtilsGeoGrid().gridGeometryInfo(geometry, boundingBox, true, sideLen);
            return new VMainParam(bounds,geometry,geometries);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private static ExecutorService newFixedThreadPool(int nThreads) {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(nThreads, nThreads, 1L, TimeUnit.MINUTES, new LinkedBlockingQueue<Runnable>());
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        return executor;
    }
}
